﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YJKRevitBase.WinForm;
using YJKRevitView;
using YJKRevitStairs.StairsEdit.DataDefine;
using YJKRevitStairs.StairsEdit.Forms;
using YJKRevitStairs.StairsEdit.Managers;

namespace YJKRevitStairs.StairsEdit
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdJoinStairs : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			this.m_commandData = commandData;
			this.uidoc = this.m_commandData.Application.ActiveUIDocument;
			this.document = this.uidoc.Document;
			CmdViewType types = (CmdViewType)127;
			if (SwitchView.SetUsableView(types, this.m_commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (CmdJoinStairs._hWndRevit == null)
			{
				CmdJoinStairs._hWndRevit = new WindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			return this.OneGroupStairs(ref message);
		}

		private Result OneGroupStairs(ref string message)
		{
			SplitStairsForm splitStairsForm = null;
			try
			{
				splitStairsForm = new SplitStairsForm("楼梯合并");
				if (splitStairsForm.ShowDialog() != DialogResult.OK)
				{
					return Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Show(ex.Message);
				return Autodesk.Revit.UI.Result.Failed;
			}
			List<Stairs> source = new List<Stairs>();
			SelectAndSearch selectAndSearch = new SelectAndSearch(this.m_commandData);
			bool flag = selectAndSearch.MutiSelectStairs("请选择要合并的楼梯: ", ref source, ref message);
			if (splitStairsForm != null)
			{
				splitStairsForm.Close();
			}
			if (!flag)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (source.LongCount<Stairs>() < 1L)
			{
				YJKMessageBox.Show("没有选中楼梯");
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			if (splitStairsForm.selMode && !selectAndSearch.SearchAllStairsByHit(ref source, ref message))
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			return this.joinStairsAndShowResult(ref source, ref message);
		}

		private Result joinStairsAndShowResult(ref List<Stairs> arStairs, ref string message)
		{
			PairingStairsCls pairingStairsCls = new PairingStairsCls(this.m_commandData, arStairs);
			List<PairsStairs> list = null;
			pairingStairsCls.PairingStairs(out list, ref arStairs);
			for (int i = list.Count - 1; i >= 0; i--)
			{
				PairsStairs pairsStairs = list[i];
				if (new JoinOnePairStairsCls(this.m_commandData, pairsStairs.stairs1, pairsStairs.stairs2).JoinOnePairStairsFun())
				{
					list.RemoveAt(i);
				}
			}
			if (arStairs.Count > 0 || list.Count > 0)
			{
				ShowStairsListForm showStairsListForm = null;
				try
				{
					showStairsListForm = new ShowStairsListForm(this.m_commandData);
					showStairsListForm.Text = "未合并楼梯列表";
					showStairsListForm.Show(CmdJoinStairs._hWndRevit);
				}
				catch (Exception ex)
				{
					YJKMessageBox.Show(ex.Message);
					if (showStairsListForm != null)
					{
						showStairsListForm.Close();
					}
					return Autodesk.Revit.UI.Result.Failed;
				}
				showStairsListForm.initList(arStairs, list);
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static WindowHandle _hWndRevit;

		private ExternalCommandData m_commandData;

		private Document document;

		private UIDocument uidoc;
	}
}
